﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using Common.Util;

namespace DataCenter.Provider
{
    public class WarrantyHistoryProvider : GenericProvider, IWarrantyHistoryProvider
    {
        public bool Add(WarrantyHistory wSms)
        {
            try
            {
                repository.Add<WarrantyHistory>(wSms);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }


        public List<WarrantyHistory> SearchWarranty(string serial = null, string model = null, bool? transferFlag = null)
        {
            var querry = repository.GetQuery<WarrantyHistory>();
            if (!serial.IsNullOrWhiteSpace())
            {
                querry = querry.Where(x => x.SerialNo.ToUpper() == serial.ToUpper());
            }

            if (!model.IsNullOrWhiteSpace())
            {
                querry = querry.Where(x => x.Model.ToUpper().Contains(model.ToUpper()));
            }

            if (transferFlag.HasValue)
            {
                string flag = transferFlag.Value.ToBooleanString();
                if (!transferFlag.Value) querry = querry.Where(x => x.SoTransferFlag.ToUpper() == flag);
                else querry = querry.Where(x => x.SoTransferFlag == null || x.SoTransferFlag.Length == 0 || x.SoTransferFlag.ToUpper() == flag);
            }

            return querry.ToList();
        }


        public bool UpdateWarrantyHistory(WarrantyHistory obj)
        {
            try
            {
                var updateRecord = RetrieveWarrantyHistory(obj.SerialNo, obj.RespType);
                if (updateRecord == null) return false;
                updateRecord.Moseq = obj.Moseq;
                updateRecord.EndUserCell = obj.EndUserCell;
                updateRecord.Model = obj.Model;
                updateRecord.MoMsgbody = obj.MoMsgbody;
                updateRecord.ReceiveDate = obj.ReceiveDate;
                updateRecord.RespMsg = obj.RespMsg;
                updateRecord.RespType = obj.RespType;
                //updateRecord.SerialNo = obj.SerialNo;
                updateRecord.ShopCell = obj.ShopCell;
                updateRecord.ShopCode = obj.ShopCode;
                updateRecord.SmsYn = obj.SmsYn;
                updateRecord.SoTransferFlag = obj.SoTransferFlag;
                if (updateRecord.SoTransferFlag.IsYes()) updateRecord.SoTransferDate = DateTime.Now;
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }


        public WarrantyHistory RetrieveWarrantyHistory(string seria, string resptype)
        {
            if (seria.IsNullOrWhiteSpace() || resptype.IsNullOrWhiteSpace()) return null;
            return repository.FindOne<WarrantyHistory>(x => x.SerialNo.ToUpper() == seria.ToUpper() && x.RespType.ToUpper() == resptype.ToUpper());
        }
    }
}
